<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "category".
 *
 * @property string $id ID
 * @property string $name 分类名
 * @property int $pid 父ID
 * @property string $created_at 创建时间
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name','is_hot'], 'required'],
            [['pid'], 'integer'],
            [['created_at'], 'integer'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public $html;
    public $level;
    public $tree_name;
    public static $tree = array();
    public static function makeCategory($categorys, $pid = 0, $level = 0){
        foreach ($categorys as $k => $category) {
            if ($category['pid'] == $pid) {
                $category['html'] = str_repeat("|--", $level);
                $category['level'] = $level;
                $category['name'] = str_repeat("　", $level).$category['html'] . " " . $category['name'];
                self::$tree[$category['id']] = $category;
                self::makeCategory($categorys,$category['id'],$level+1);
            }
        }
        return self::$tree;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '分类名',
            'pid' => '父ID',
            'is_hot'=>'是否热门',
            'created_at' => '创建时间',
        ];
    }
}
